﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class KetQuaTimKiem : System.Web.UI.Page
{
    private string loaisp = "";
    private string nsx = "";
    private string tukhoa = "";
    private string giatu = "";
    private string den = "";
    private DataSet objDataSet;
    protected void Page_Load(object sender, EventArgs e)
    {
        // don't reload data during postbacks
        if (!IsPostBack)
        {
            // set the title of the page
            this.Title = TGDDConfiguration.SiteName +
                         ": Tim kiem";

            loaisp = Request.QueryString["loaisp"] == null ? "" : Request.QueryString["loaisp"];
            nsx = Request.QueryString["nsx"] == null ? "" : Request.QueryString["nsx"];
            tukhoa = Request.QueryString["tukhoa"] == null ? "" : Request.QueryString["tukhoa"];
            giatu = Request.QueryString["giatu"] == null ? "" : Request.QueryString["giatu"];
            den = Request.QueryString["den"] == null ? "" : Request.QueryString["den"];
            LoadSanPhamTimThay();
        }
    }

    private SanPhamCRT sanphanCrt;
    private void CapNhatTieuChuanTraCuu()
    {
        sanphanCrt = new SanPhamCRT();
        if(!loaisp.Equals(""))
        {
            sanphanCrt.Loaisp = int.Parse(loaisp);
        }
        if (!nsx.Equals(""))
        {
            sanphanCrt.Nsx = int.Parse(nsx);
        }
        if (!tukhoa.Equals(""))
        {
            sanphanCrt.Tukhoa = tukhoa;
        }
        if (!giatu.Equals(""))
        {
            sanphanCrt.Giatu = long.Parse(giatu);
        }
        if (!den.Equals(""))
        {
            sanphanCrt.Den = int.Parse(den);
        }
    }
    public void LoadSanPhamTimThay()
    {
        int howManyPages = 1;
        string page = Request.QueryString["Page"];
        if (page == null)
        {
            page = "1";
        }
        CapNhatTieuChuanTraCuu();
        SanPhamBUS objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objSanPham.TimKiemSanPham(sanphanCrt);

        //lay tong so trang
        int tongsosp = objDataSet.Tables[0].Rows.Count;
        howManyPages = (int)Math.Ceiling((double)tongsosp /
                         (double)TGDDConfiguration.ProductsPerPage);
        lblSoSanPham.Text = tongsosp.ToString();

        objSanPham = new SanPhamBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = objSanPham.TimKiemSanPham_Paging(sanphanCrt,
            int.Parse(page), TGDDConfiguration.ProductsPerPage);

        repSanPham.DataSource = objDataSet.Tables[0];
        repSanPham.DataBind();


        if (howManyPages > 1)
        {
            // have the current page as integer
            int currentPage = Int32.Parse(page);
            // make controls visible
            lblPaging.Visible = true;
            previousLink.Visible = true;
            nextLink.Visible = true;
            // set the paging text
            lblPaging.Text = "Page " + page + " of " + howManyPages.ToString();
            // create the Previous link
            if (currentPage == 1)
                previousLink.Enabled = false;
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                {
                    if (query.AllKeys[i] != null)
                    {
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                        {
                            newQueryString += paramName + "=" + query[i] + "&";
                        }
                    }
                }
                //this.previousLink.Attributes.Add("onclick", "javascript:GoToPageKetQuaTimKiem("+ (currentPage - 1) + ");");
                previousLink.NavigateUrl =
                    Request.Url.AbsolutePath + newQueryString +
                    "Page=" + (currentPage - 1).ToString();
            }
            // create the Next link
            if (currentPage == howManyPages)
                nextLink.Enabled = false;
            else
            {
                NameValueCollection query = Request.QueryString;
                string paramName, newQueryString = "?";
                for (int i = 0; i < query.Count; i++)
                {
                    if (query.AllKeys[i] != null)
                    {
                        if ((paramName = query.AllKeys[i].ToString()).ToUpper() != "PAGE")
                        {
                            newQueryString += paramName + "=" + query[i] + "&";
                        }
                    }
                }
                //this.nextLink.Attributes.Add("onclick", "javascript:GoToPageKetQuaTimKiem(" + (currentPage + 1) + ");");
                nextLink.NavigateUrl =
                    Request.Url.AbsolutePath + newQueryString +
                    "Page=" + (currentPage + 1).ToString();
            }
        }


    }

    private void MessageBox(string msg)
    {
        Page.Controls.Add(new LiteralControl(
         "<script language='javascript'> window.alert('" + msg.Replace("'", "\\'") + "');</script>"));
    }

    protected void repSanPham_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (Session["g_UserId"] == null)
        {
            MessageBox("Vui lòng đăng nhập để mua hàng!");
            return;
        }
        // Add the product to the shopping cart
        GioHangBUS objShoppingCart =
            new GioHangBUS(TGDDConfiguration.DbConnectionString);
        GioHangDTO shoppingCartDto = new GioHangDTO();
        shoppingCartDto.Id = AccessGioHang.idGioHang;
        shoppingCartDto.IdSanPham = int.Parse(e.CommandArgument.ToString());

        try
        {
            // Execute the insert command
            bool success = objShoppingCart.AddItemToGioHang(shoppingCartDto);
        }
        catch (System.Exception ex)
        {
            throw new Exception(ex.Message, ex.InnerException);
        }
    }
}
